//
//  SY_Today_AttendanceViewController.m
//  ChatDemo-UI2.0
//
//  Created by Sylar on 2017/2/20.
//  Copyright © 2017年 Sylar. All rights reserved.
//

#import "SY_Today_AttendanceViewController.h"
#import "SY_Today_AttCell.h"
#import "HomeParLeaveDetailController.h"


@interface SY_Today_AttendanceViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *myTableView;

@property (nonatomic, strong) NSMutableArray *dateArr;

@end

@implementation SY_Today_AttendanceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navBar.nav_titleLabel.text = @"当日考勤";
    
    [self.view addSubview:self.myTableView];
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Request

- (void)loadData {
    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:@"api_rollcalldate_info" forKey:@"method"];
    [dict setObject:self.classID forKey:@"classid"];
    [dict setObject:self.dateStr forKey:@"datetime"];
    
    [Utils get:nil parameters:dict success:^(NSInteger recode, id resposeJson){
        
        NSDictionary *dict = [(NSDictionary *)resposeJson objectForKey:@"re_data"];
        
        self.dateArr = [[NSMutableArray alloc]init];
       
       NSArray * arr = [dict objectForKey:@"leavelist"];
       
       if ([arr isKindOfClass:[NSNull class]]) {
          
          arr = nil;
       }
        if ([arr count] > 0) {
           
            [self.dateArr addObject:[dict objectForKey:@"leavelist"]];
        }
       
       NSArray * kkarr = [dict objectForKey:@"kklist"];
       
       if ([kkarr isKindOfClass:[NSNull class]]) {
          
          kkarr = nil;
       }
       
        if ([kkarr count] > 0) {
           
            [self.dateArr addObject:[dict objectForKey:@"kklist"]];
        }
        
        if (self.dateArr.count == 0) {
            
            UILabel *emptyLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 14 + 44, SCREENWIDTH, 16)];
            emptyLabel.textColor = colorFromHex(@"#3b3b3b");
            emptyLabel.text = @"今日全勤，无异常情况";
            [emptyLabel setFont:[UIFont systemFontOfSize:16]];
            [self.myTableView addSubview:emptyLabel];
        }
        [self.myTableView reloadData];

    } failure:^(NSInteger recode, NSError * error){
        
    }];
    
}

#pragma mark - TableViewDateSource & TableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dateArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [[self.dateArr objectAtIndex:section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 26;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 26)];
    [headView setBackgroundColor:colorFromHex(@"#F5F8FA")];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 25.5, SCREENWIDTH, 0.5)];
    [lineView setBackgroundColor:colorFromHex(@"#DEDEDE")];
    [headView addSubview:lineView];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, SCREENWIDTH, 26)];
    [titleLabel setFont:[UIFont systemFontOfSize:14]];
    [titleLabel setTextColor:colorFromHex(@"#1ebfff")];
    [headView addSubview:titleLabel];
    
    if ([[[[self.dateArr objectAtIndex:section] objectAtIndex:0] objectForKey:@"eventname"] isEqualToString:@"旷课"]) {
        titleLabel.text = @"旷课";
    }else {
        titleLabel.text = @"请假";
    }
    
    return headView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SY_Today_AttCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
    
    if (cell == nil) {
        cell = [[SY_Today_AttCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellIdentifier"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if (![[[[self.dateArr objectAtIndex:indexPath.section] objectAtIndex:0] objectForKey:@"eventname"] isEqualToString:@"旷课"]) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    [cell creatCell:[[self.dateArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
    
    
    if (indexPath.row == [[self.dateArr objectAtIndex:indexPath.section] count] - 1) {
        [cell.lineView setX:0];
    }else {
        [cell.lineView setX:15];
    }
    
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (![[[[self.dateArr objectAtIndex:indexPath.section] objectAtIndex:0] objectForKey:@"eventname"] isEqualToString:@"旷课"]) {
        
        HomeParLeaveDetailController *vc = [[HomeParLeaveDetailController alloc] init];
        
        vc.leaveid = [[[[self.dateArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"eventid"] integerValue];
        
        [self.navigationController pushViewController:vc animated:YES];
        
    }
}


#pragma mark - Get

- (UITableView *)myTableView {
    
    if (_myTableView == nil) {
        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH, SCREENHEIGH - SafeAreaTopHeight) style:(UITableViewStylePlain)];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.rowHeight = 44;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 44)];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, SCREENWIDTH - 15, 44)];
        label.textColor = colorFromHex(@"#3b3b3b");
        [label setFont:[UIFont systemFontOfSize:16]];
        
        NSDateFormatter *formatter1 =[[NSDateFormatter alloc]init];
        [formatter1 setDateFormat:@"yyyy年MM月dd日"];
        NSString *dateString = [formatter1 stringFromDate:self.selectDate];
        
        label.text = [NSString stringWithFormat:@"%@%@考勤情况",dateString,self.className];
        
        [headView addSubview:label];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 43.5, SCREENWIDTH, 0.5)];
        [lineView setBackgroundColor:colorFromHex(@"#DEDEDE")];
        [headView addSubview:lineView];
        
        
        _myTableView.tableHeaderView = headView;
        
        
    }
    return _myTableView;
}

@end
